package ae.pcfc.cldos.online.ws.ep.endpoint;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import ae.pcfc.cldos.online.common.clddb.domain.ApplicationStatus;
import ae.pcfc.cldos.online.common.clddb.domain.Process;
import ae.pcfc.cldos.online.common.clddb.domain.ServiceMaster;
import ae.pcfc.cldos.online.common.delegate.GSUpdateStatusDelegate;
import ae.pcfc.cldos.online.common.delegate.GsIntegrationDelegate;
import ae.pcfc.cldos.online.common.vo.AppMetaDataVO;
import ae.pcfc.cldos.online.common.vo.employee.details.EmployeeDetailsVO;
import ae.pcfc.cldos.online.ws.ep.constants.EpConstants;
import ae.pcfc.cldos.online.ws.ep.types.AppInfo;
import ae.pcfc.cldos.online.ws.ep.types.ApplicationRequestDTO;
import ae.pcfc.cldos.online.ws.ep.types.EmployeeInfo;
import ae.pcfc.cldos.online.ws.ep.types.EpTaskStartRequest;
import ae.pcfc.cldos.online.ws.ep.types.EpTaskStartResponse;
import ae.pcfc.cldos.online.ws.ep.types.GetOnlineAppIdRequest;
import ae.pcfc.cldos.online.ws.ep.types.GetOnlineAppIdResponse;
import ae.pcfc.cldos.online.ws.ep.types.UpdateAppStatusRequest;
import ae.pcfc.cldos.online.ws.ep.types.UpdateAppWorkFlowStatusRequest;

/**
 * 
 * @author arosa
 *
 */
@Endpoint
public class EpTaskStartEndPoint {
	
	@Autowired
	@Qualifier("gsIntegrationDelegate")
	private GsIntegrationDelegate gsIntegrationDelegate;
	
	@Autowired
	@Qualifier("gSUpdateStatusDelegate")
	private GSUpdateStatusDelegate gSUpdateStatusDelegate;
	
	private static final String TASK_TYPE_RENEWAL = "REN";
	private static final String TASK_TYPE_CANCELATION = "CAN";
	private static final String EXIT_PAPER_SUBM = "EXPAPSUBM";
	
	private static final int ONE_MONTH = 1;
	private static final int THREE_MONTHS = 3;
	
	private Map<String, Long> empSourceMapping = new HashMap<String, Long>();
	
	public EpTaskStartEndPoint(){
		empSourceMapping.put("1", 0l);
		empSourceMapping.put("2", 1l);
		empSourceMapping.put("3", 3l);
		empSourceMapping.put("6", 6l);
	}
	
	/**
	 * 
	 * @param edEpTaskStartRequest
	 * @return
	 * @throws Exception
	 */
	@PayloadRoot(namespace = EpConstants.NAMESPACE_URI, localPart = EpConstants.START_TASK_LOCAL_PART)
	public @ResponsePayload EpTaskStartResponse epStartTask(
				@RequestPayload EpTaskStartRequest epEpTaskStartRequest) throws Exception {
		
		AppInfo appInfo = epEpTaskStartRequest.getAppInfo();
		
		AppMetaDataVO appMetaDataVO = new AppMetaDataVO();
		appMetaDataVO.setWfStatus(appInfo.getWfStatus());
		appMetaDataVO.setWfIncidentId(appInfo.getWfIncidentId());
		appMetaDataVO.setCoLicNo(appInfo.getCompanyLicNo());
		appMetaDataVO.setServiceCode(empSourceMapping.get(appInfo.getServiceId()));
		appMetaDataVO.setProcessCode(appInfo.getProcessCode());
		appMetaDataVO.setClientRefNo(appInfo.getClientReferenceNumber());
		appMetaDataVO.setWfInitiatedBy(appInfo.getWfInitiatedBy());
		
		String taskType = appInfo.getTaskType();
		if(taskType.equals(TASK_TYPE_CANCELATION)){
			appMetaDataVO.setStatusCode(ApplicationStatus.PAYMENT_CANCELATION);
		} else if(taskType.endsWith(TASK_TYPE_RENEWAL)){
			appMetaDataVO.setStatusCode(ApplicationStatus.PAYMENT_RENEWAL);
		} else if(taskType.equals(EXIT_PAPER_SUBM)){
			appMetaDataVO.setStatusCode(ApplicationStatus.STATUS_EXIT_PAPER_SUBMIT);
		} else {
			throw new Exception("BAD TASK TYPE");
		}
		
		EmployeeDetailsVO employeeDetailsVO = new EmployeeDetailsVO();
		EmployeeInfo employeeInfo = epEpTaskStartRequest.getEmployeeInfo();
		
		employeeDetailsVO.setEdPsprtName(employeeInfo.getFName() + " " + employeeInfo.getLName());
		employeeDetailsVO.setEmployeeId(employeeInfo.getEmpId());
		
		gsIntegrationDelegate.startTask(appMetaDataVO, employeeDetailsVO);
		
		EpTaskStartResponse epTaskStartResponse = new EpTaskStartResponse();
		epTaskStartResponse.setIsSuccess(true);
		return epTaskStartResponse;
	}
	
	@PayloadRoot(namespace = EpConstants.NAMESPACE_URI, localPart = EpConstants.GET_ONLINE_APP_ID_LOCAL_PART)
	public @ResponsePayload GetOnlineAppIdResponse epStartTask(
				@RequestPayload GetOnlineAppIdRequest getOnlineAppIdRequest) throws Exception {
	 
		ApplicationRequestDTO applicationRequestDTO = getOnlineAppIdRequest.getApplicationRequestDTO();
		
		AppMetaDataVO appMetaDataVO = new AppMetaDataVO();
		appMetaDataVO.setWfStatus(applicationRequestDTO.getWfStatus());
		appMetaDataVO.setWfIncidentId(applicationRequestDTO.getWfIncidentId());
		appMetaDataVO.setCoLicNo(applicationRequestDTO.getCompanyLicNo());
		appMetaDataVO.setProcessCode(applicationRequestDTO.getProcessCode());
		appMetaDataVO.setClientRefNo(applicationRequestDTO.getClientReferenceNumber());
		appMetaDataVO.setWfInitiatedBy(applicationRequestDTO.getWfInitiatedBy());
		
		String processCode = appMetaDataVO.getProcessCode();
		
		if(processCode.equals(Process.PROCESS_EMPLOYMENT_VISA)){
			appMetaDataVO.setServiceCode(empSourceMapping.get(applicationRequestDTO.getEmpSource()));
		} else if(processCode.equals(Process.PROCESS_CANCEL_VISIT_VISA)){
			appMetaDataVO.setServiceCode(ServiceMaster.CANCEL_VV);
		} else if(processCode.equals(Process.PROCESS_EMPLOYMENT_VISA_CANCELATION)){
			appMetaDataVO.setServiceCode(ServiceMaster.EV_CANCELATION);
		} else if(processCode.equals(Process.PROCESS_EMPLOYMENT_VISA_RENEWAL)){
			appMetaDataVO.setServiceCode(ServiceMaster.EV_RENEVAL);
		} else if(processCode.equals(Process.PROCESS_NOC_LETTERS)){
			appMetaDataVO.setServiceCode(ServiceMaster.NOC_LETTER);
		} else if(processCode.equals(Process.PROCESS_VISIT_VISA)){
			if(applicationRequestDTO.getVisitVisaDuration()==ONE_MONTH){
				appMetaDataVO.setServiceCode(ServiceMaster.NV_ONE_MONTH);
			} else if(applicationRequestDTO.getVisitVisaDuration()==THREE_MONTHS){
				appMetaDataVO.setServiceCode(ServiceMaster.NV_THREE_MONTH);
			}
		}
		
		GetOnlineAppIdResponse getOnlineAppIdResponse = new GetOnlineAppIdResponse();
		Long onlineAppId = gsIntegrationDelegate.getOnlineApplicationId(appMetaDataVO);
		getOnlineAppIdResponse.setOnlineAppId(onlineAppId);
		
		return getOnlineAppIdResponse;
	}
	

	/**
	 * @param updateAppStatusRequest
	 * @throws Exception
	 */
	@PayloadRoot(namespace = EpConstants.NAMESPACE_URI, localPart = EpConstants.UPDATE_APP_STATUS)
	public void updateAppStatus(
				@RequestPayload UpdateAppStatusRequest updateAppStatusRequest) throws Exception {
		String clientRefNo = updateAppStatusRequest.getClientReferenceNumber();
		Long statusCode = updateAppStatusRequest.getStatusCode();
		gSUpdateStatusDelegate.changeAppStatus(clientRefNo, statusCode);
	
	}
	
	/**
	 * @param upAppWorkFlowStatusRequest
	 * @throws Exception
	 */
	@PayloadRoot(namespace = EpConstants.NAMESPACE_URI, localPart = EpConstants.UPDATE_APP_WF_STATUS)
	public void updateAppWorkFlowStatus(
				@RequestPayload UpdateAppWorkFlowStatusRequest upAppWorkFlowStatusRequest ) throws Exception {
		String clientRefNo = upAppWorkFlowStatusRequest.getClientReferenceNumber();
		String wfStatus = upAppWorkFlowStatusRequest.getWfStatus();
		gSUpdateStatusDelegate.changeAppWorkFlowStatus(clientRefNo, wfStatus);
	
	}
	
}
